package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.qiniu.util.Json;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RequestMapping("/member")
@RestController
public class MemberController {

    @Reference
    private MemberService memberService;

    @Autowired
    private JedisPool jedisPool;

    //使用手机号和验证码登录
    @RequestMapping("/login")
    public Result login(HttpServletResponse response, @RequestBody Map map) throws Exception {
        //获取手机号
        String telephone = (String) map.get("telephone");
        //获取用户输入验证码
        String validateCode = (String) map.get("validateCode");
        //从redis中获取保存得验证码，进行比较
        Jedis resource = jedisPool.getResource();
        String validateCodeInRedis = resource.get(telephone + RedisMessageConstant.SENDTYPE_LOGIN);

        if (validateCodeInRedis != null && validateCode != null && validateCode.equals(validateCodeInRedis)) {
            //验证码输入正确
            //判断当前用户是否为会员
            Member member = memberService.findByTelephone(telephone);

            if (member == null) {
                //如果不是会员，自动注册为会员，将用户信息保存到会员表
                member = new Member();
                member.setRegTime(new Date());
                member.setPhoneNumber(telephone);
                memberService.add(member);
            }
            //是会员，向客户浏览器写入cookie，内容为手机号
            Cookie cookie = new Cookie("login_member_telephone", telephone);
            cookie.setPath("/");
            //保存时间为30天
            cookie.setMaxAge(60 * 60 * 24 * 30);
            response.addCookie(cookie);
            //将会员信息保存到Redis
            String json = JSON.toJSON(member).toString();
            resource.setex(telephone, 300, json);
            resource.close();

            return new Result(true, MessageConstant.LOGIN_SUCCESS);
        } else {
            //验证码输入错误
            resource.close();
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }
    }

    /**
     * 根据手机号码查询预约信息
     * @param request
     * @return
     */
    @RequestMapping("/getsetmealListByPhoneNumber")
    public Result getsetmealListByPhoneNumber(HttpServletRequest request) {
        try {
            String phoneNumber = (String)request.getSession().getAttribute("phoneNumber");
            if (phoneNumber==null||phoneNumber==""){
                phoneNumber  = "18511279942";
            }
            System.out.println(phoneNumber);
            List<Map> map =  memberService.getsetmealListByPhoneNumber(phoneNumber);
            return new Result(true,MessageConstant.QUERY_SETMEALLIST_SUCCESS,map);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SETMEALLIST_FAIL);
        }
    }


}
